package design_principles.chain_of_responsibility_pattern.src.main.java.com.code;

import design_principles.chain_of_responsibility_pattern.src.main.java.com.code.vo.Level;
import design_principles.chain_of_responsibility_pattern.src.main.java.com.code.vo.Request;
import design_principles.chain_of_responsibility_pattern.src.main.java.com.code.vo.Response;

public abstract class Handler {
    private Handler nextHandler;

    public final Response handleMessage(Request request) {
        Response response = null;
        if (this.getHandlerLevel().equals(request.getRequestLevel())) {
            response = this.echRespose(request);
        } else {
            // 判断是否有下一个处理者
            if (this.nextHandler != null) {
                response = this.nextHandler.handleMessage(request);
            } else {// 不属于自己的处理级别
                    // 没有适当的处理者，业务自行处理
            }

        }
        return response;

    }

    // 设置下一个处理者是谁
    public void setNext(Handler handler) {
        this.nextHandler = handler;
    }

    // 每个处理者都有一个处理级别
    protected abstract Level getHandlerLevel();

    // 每个处理者都必须实现处理任务
    protected abstract Response echRespose(Request request);

}